﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Leadtools;
using Leadtools.Codecs;
using Leadtools.WinForms;
using System.IO;

namespace MINERP.ComControls
{
    public partial class BigPicViewer : Form
    {
        //属性
        /// <summary>
        /// 默认第一个加载照片的索引(索引从0开始)
        /// </summary>
        public int CurrentIndex { get; set; }                                   //默认的第一张照片索引
        /// <summary>
        /// 所有照片的集合
        /// </summary>
        public List<string> PhotoPaths { get; set; }                            //所有照片的路径,及对应的照片条码

        //参数
        private int TotalPage;

        public BigPicViewer()
        {
            InitializeComponent();
        }

        private void BigPicViewer_Load(object sender, EventArgs e)
        {
            if (PhotoPaths == null || PhotoPaths.Count == 0)
                return;
            TotalPage = PhotoPaths.Count/2;
            AddPhoto(PhotoPaths[2*CurrentIndex],PhotoPaths[2*CurrentIndex+1]);
            SetCurrentAndTotalPages(CurrentIndex+1);
            SetCanUse();
        }

        #region 加载单张照片
        /// <summary>
        /// 加载单张照片
        /// </summary>
        /// <param name="photoPath"></param>
        private void AddPhoto(string photoPath,string photoBarCode)
        {
            if (!File.Exists(photoPath))
            {
                MessageBox.Show(photoPath+"\n文件不存在!");
                return;
            }

            try
            {
                RasterCodecs.Startup();
                RasterCodecs codecs = new RasterCodecs();
                CodecsImageInfo info = codecs.GetInformation(photoPath, true);
                int resolution = info.XResolution;
                string height = string.Format("{0:0.####}", info.Height / (double)resolution);
                string width = string.Format("{0:0.####}", info.Width / (double)resolution);
                //照片基本信息
                if (photoBarCode == "")
                {
                    int index = photoPath.LastIndexOf("\\");
                    string pname = photoPath.Substring(index + 1);
                    lblPhotoName.Text = pname;
                }
                else
                    lblPhotoName.Text = photoBarCode;
                lblPhotoHeight.Text = height;
                lblPhotoWidth.Text = width;
                //加载原始照片
                RasterImage img = codecs.Load(photoPath);
                imgViewer.SizeMode = RasterPaintSizeMode.FitAlways;
                imgViewer.HorizontalAlignMode = RasterPaintAlignMode.Center;
                imgViewer.VerticalAlignMode = RasterPaintAlignMode.Center;
                imgViewer.Image = img;
                codecs.Dispose();
                RasterCodecs.Shutdown();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //根据鼠标单击位置，转到上一张或下一张
        private void imgViewer_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                int viewerWidth = imgViewer.Width;
                int mid = viewerWidth / 2;
                if (e.X <= mid)
                {
                    //加载下一张照片
                    CurrentIndex--;
                    if (CurrentIndex < 0)
                    {
                        CurrentIndex++;
                        return;
                    }
                    string photoPath = PhotoPaths[2*CurrentIndex];
                    string photoBarCode = PhotoPaths[2 * CurrentIndex + 1];
                    AddPhoto(photoPath,photoBarCode);
                    SetCurrentAndTotalPages(CurrentIndex + 1);
                }
                else
                {
                    //加载上一张照片
                    CurrentIndex++;
                    if (CurrentIndex > PhotoPaths.Count/2 - 1)
                    {
                        CurrentIndex--;
                        return;
                    }
                    string photoPath = PhotoPaths[2 * CurrentIndex];
                    string photoBarCode = PhotoPaths[2 * CurrentIndex + 1];
                    AddPhoto(photoPath, photoBarCode);
                    SetCurrentAndTotalPages(CurrentIndex + 1);
                }
                SetCanUse();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void SetCurrentAndTotalPages(int currentPage)
        {
            lblCurrentPhoto.Text = currentPage.ToString() + "/" + TotalPage.ToString();
        }

        //上一张按钮
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                CurrentIndex--;
                if (CurrentIndex < 0)
                {
                    CurrentIndex++;
                    return;
                }
                string photoPath = PhotoPaths[2 * CurrentIndex];
                string photoBarCode = PhotoPaths[2 * CurrentIndex + 1];
                AddPhoto(photoPath, photoBarCode);
                SetCurrentAndTotalPages(CurrentIndex + 1);
                SetCanUse();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //下一张按钮
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                CurrentIndex++;
                if (CurrentIndex > PhotoPaths.Count/2 - 1)
                {
                    CurrentIndex--;
                    return;
                }
                string photoPath = PhotoPaths[2 * CurrentIndex];
                string photoBarCode = PhotoPaths[2 * CurrentIndex + 1];
                AddPhoto(photoPath, photoBarCode);
                SetCurrentAndTotalPages(CurrentIndex + 1);
                SetCanUse();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 设置上一张与下一张是否可用
        /// </summary>
        public void SetCanUse()
        {
            try
            {
                if (CurrentIndex <= 0)
                {
                    btnPrevious.Enabled = false;
                }
                else
                {
                    btnPrevious.Enabled = true;
                }
                if (CurrentIndex >= PhotoPaths.Count/2 - 1)
                {
                    btnNext.Enabled = false;
                }
                else
                {
                    btnNext.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
